<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" class="nav">
      <el-breadcrumb-item to="/welcome"
        ><strong>首页</strong></el-breadcrumb-item
      >
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表 </el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div class="btn"><el-button type="primary">添加角色</el-button></div>
      <!--表格-->
      <el-table :data="list" class="table" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['bdbottom', i1 === 0 ? 'bdtop' : '', 'vcenter']"
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
            >
              <!-- 渲染一级权限 -->
              <el-col :span="6">
                <el-tag closable @close="removeId(scope.row, item1.id)">{{
                  item1.authName
                }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 渲染二级和三级权限 -->
              <el-col :span="18">
                <!-- 通过 for 循环 嵌套渲染二级权限 -->
                <el-row
                  :class="[i2 === 0 ? '' : 'bdtop', 'vcenter']"
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeId(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      @close="removeId(scope.row, item3.id)"
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" icon="el-icon-delete"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="showSetRightDialog(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分配权限的对话框 -->
      <el-dialog
        title="分配权限"
        :visible.sync="rightsDialogVisible"
        width="50%"
        @close="close"
      >
        <!-- 树形控件 -->
        <!-- <el-tree
          :data="rightslist"
          :props="treeProps"
          show-checkbox
          node-key="id"
          default-expand-all
          :default-checked-keys="defKeys"
          ref="treeRef"
        ></el-tree> -->

        <el-tree
          :data="rightslist"
          show-checkbox
          node-key="id"
          :default-checked-keys="chkyes"
          :props="treeProps"
          default-expand-all
          ref="treeRef"
        >
        </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="rightsDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotRights">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import { rolessApi, rightslistApi, delRightsApi, addRightsApi } from '@/api/rights'
export default {
  name: 'Rolelist',
  data () {
    return {
      // 所有角色列表数据
      list: [],
      // 树形控件的属性绑定对象
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      // 默认选中的节点Id值数组
      chkyes: [],
      // 所有权限列表数据
      rightslist: [],
      rightsDialogVisible: false,
      jueseID: ''
    }
  },
  created () {
    this.getrolessApi()
  },
  computed: {
  },
  methods: {
    // 获取所有的角色列表
    async getrolessApi () {
      const res = await rolessApi()
      this.list = res.data
      console.log(res, '所有角色列表')
    },
    // 删除权限
    removeId (row, perId) {
      console.log(row, perId)
      this.$confirm('此操作将删除权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await delRightsApi(row.id, perId)
        console.log(res, '已经删除了')
        row.children = res.data// 重新给三级标签赋值
        this.$message({
          type: 'success',
          message: res.meta.msg
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 递归循环父级数组,取得所有第三级权限ID
    digui (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => this.digui(item, arr))
    },
    // 打开分配权限对话框
    async showSetRightDialog (row) {
      this.jueseID = row.id
      console.log(row.children)
      const res = await rightslistApi()
      // console.log(res);
      this.rightslist = res.data
      this.digui(row, this.chkyes)
      // console.log(this.chkyes, 22233223);
      this.rightsDialogVisible = true
    },

    // 确定分配权限
    async allotRights () {
      const count = [...this.$refs.treeRef.getCheckedKeys(), ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const counts = count.join(',')
      // console.log(counts);

      // console.log(count, '角色权限ID');
      await addRightsApi(this.jueseID, counts)
      this.rightsDialogVisible = false
      // console.log(res);
    },
    // 关闭分配权限对话框
    close () {
      this.chkyes = []
    }
  }
}
</script>
<style lang='less'  scoped>
.nav {
  font-size: 12px;
  margin-bottom: 15px;
}
.btn {
  margin-bottom: 15px;
}
.table {
  font-size: 12px;
}

.el-tag {
  margin: 7px;
}

.bdtop {
  border-top: 1px solid #eee;
}

.bdbottom {
  border-bottom: 1px solid #eee;
}

.vcenter {
  display: flex;
  align-items: center;
}
</style>
